//
//  XHRAddMedicalRecordViewController.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/6.
//
//

#import "XHRAddMedicalRecordViewController.h"
#import "XHRAddMedicalCell.h"
#import "XHRSelectIllCategoryViewController.h"
#import <SVProgressHUD.h>
#import "XHRMedicalRecordItem.h"
@interface XHRAddMedicalRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
/**顶部tableView*/
@property(nonatomic,weak)UITableView *tableView;

/**数据模型*/
@property(nonatomic,strong)XHRMedicalRecordItem *medicalRecordItem;
@end

@implementation XHRAddMedicalRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSettings];
}
- (void)initSettings
{
    self.view.backgroundColor = XHRColorWithRGB(242, 242, 248);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(didClickCommitButton:)];
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickCommitButton:(UIBarButtonItem *)sender
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    self.medicalRecordItem.time = [formatter stringFromDate:[NSDate date]];
    if (self.addmedicalViewControllerBlock) {
        self.addmedicalViewControllerBlock(self.medicalRecordItem);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark - 布局子控件
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.offset(0);
        make.top.offset(0);
        make.height.equalTo(XHRScreenHeight * 0.75);
    }];
}
#pragma mark -
#pragma mark - tableViewDataSoruce
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHRAddMedicalCell *cell = [XHRAddMedicalCell addMedicalCellWithTableView:tableView indexPath:indexPath];
    if (indexPath.section == 1) {
        __weak typeof(self) weakSelf = self;
        if (indexPath.row  == 0) {
            
            cell.addMedicalCellBlock = ^(NSString *text,UIImage *icon){
                weakSelf.medicalRecordItem.detailDescription = text;
            };
        }
        else if (indexPath.row == 1)
        {
            cell.addMedicalCellBlock = ^(NSString *text,UIImage *image)
            {
                weakSelf.medicalRecordItem.image = image;
            };
        }
    }
    return cell;
}
#pragma mark -
#pragma mark - tableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)return;
    
    if (indexPath.section == 0) {
        XHRSelectIllCategoryViewController *selectIllViewController = [[XHRSelectIllCategoryViewController alloc]init];
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:index];
        if (!cell.detailTextLabel.text && indexPath.row != 0) {
            [SVProgressHUD setMinimumDismissTimeInterval:0.1];
            [SVProgressHUD showErrorWithStatus:@"请先选择疾病类型"];
            return;
        }
        if (indexPath.row == 0)
        {
            selectIllViewController.dataArray = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科"];
            
        }
        else if (indexPath.row == 1)
        {
            selectIllViewController.dataArray = @[@"工藤新一",@"黑羽盗一",@"赤井秀一",@"铃木园子",@"工藤有希子",@"佐藤美和子",@"中森青子",@"小泉红子",@"桃井惠子",@"冲野洋子",@"毛利兰",@"灰原哀",@"黑羽快斗"];
        }
        __weak typeof(self)weakSelf = self;
        selectIllViewController.selectIllCategoryViewBlock = ^(NSString *title)
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.detailTextLabel.text = title;
            if (indexPath.row == 0) {
                weakSelf.medicalRecordItem.illType = title;
            }
            else if (indexPath.row == 1)
            {
                weakSelf.medicalRecordItem.baseType = title;
            }
        };
        
        [self.navigationController pushViewController:selectIllViewController animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) return 50.0;
    else  return 185;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
#pragma mark -
#pragma mark - 懒加载控件
- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
        [self.view addSubview:tableView];
        _tableView = tableView;
        
        tableView.backgroundColor = [UIColor clearColor];
        tableView.dataSource = self;
        tableView.delegate = self;
    }
    return _tableView;
}
- (XHRMedicalRecordItem *)medicalRecordItem
{
    if (!_medicalRecordItem) {
        _medicalRecordItem = [[XHRMedicalRecordItem alloc]init];
    }
    return _medicalRecordItem;
}
@end
